package com.sunfeng.netty.atguigu.protocoltcp2;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @author 孙峰
 * @Description: TODO
 * @date 2020/12/11 10:54
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int bytes = in.readableBytes();
        byte[] contentBytes = new byte[bytes];
        in.readBytes(contentBytes);

        UserProtocol userProtocol = new UserProtocol();
        userProtocol.setLength(bytes);
        userProtocol.setContent(contentBytes);
        ctx.writeAndFlush(userProtocol);
    }
}
